package com.sy.simpleegg.platform.kkb.network

import android.annotation.SuppressLint
import com.sy.simpleegg.platform.kkb.constant.Constant
import com.sy.simpleegg.platform.kkb.network.base.BaseResult
import com.sy.simpleegg.platform.kkb.network.config.Constants
import com.sy.simpleegg.platform.kkb.network.errorhandler.ExceptionHandle
import io.reactivex.functions.Function
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.Interceptor

/**
 * @author sy
 * @date 2024/10/11 14:55
 * @desc
 */
class ApiWrapper : NetWorkApi() {
    companion object {
        @SuppressLint("StaticFieldLeak")
        var instance: ApiWrapper? = null

        fun singleInstance(): ApiWrapper {
            if (instance == null) {
                synchronized(ApiWrapper::class) {
                    if (instance == null) {
                        instance = ApiWrapper()
                    }
                }
            }
            return instance!!
        }
    }

//    companion object{
//        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
//            ApiWrapper()
//        }
//    }


    fun setIsDebug(isDebug: Boolean): ApiWrapper {
        mBaseUrl

        return this
    }

    fun cleanApiWrapper() {
//        if (instance != null) {
//            instance!!.cleanCash()
//            instance = null
//        }
    }

    override fun <T> getAppErrorHandler(): Function<T, T>? {
        return Function { response: T ->
            if (response is BaseResult<*> && (response as BaseResult<*>)
                    .status != 0L
            ) {
                val msg = if ((response as BaseResult<*>).msg != null
                ) (response as BaseResult<*>).msg else "服务器数据异常"
                val exception =
                    ExceptionHandle.ServerException(msg)
                exception.code = (response as BaseResult<*>).status
                exception.message = msg
                throw exception
            } else {
                return@Function response
            }
        }
    }

    override fun interceptorList(): List<Interceptor>? {
        return null
    }

    override fun getRetryCount(): Int {
        return Constants.RETRY_COUNT
    }

    override fun <T> getService(service: Class<T>): T {
        return singleInstance().getRetrofit(service).create(service)
    }

    override fun getFormalUrl(): String {
        return getUrl(if (Constant.IS_TEST) Constants.BASE_BUSINESS_URL_TEST else Constants.BASE_BUSINESS_URL_PRO)
    }

    override fun getTestUrl(): String {
        return getUrl(if (Constant.IS_TEST) Constants.BASE_BUSINESS_URL_TEST else Constants.BASE_BUSINESS_URL_PRO)
    }

    fun getUrl(url: String?): String {
        var url = url
        return if (url != null && !url.isEmpty()) {
            if (!url.contains("http")) {
                url = "http://$url"
            }
            url
        } else {
            throw NullPointerException("服务器地址不可以为空")
        }
    }

    fun checkUrl(url: String): Boolean {
        val parse = url.toHttpUrlOrNull()
        return parse != null
    }
}